#include "mycoin.h"
#include <QDebug>

MyCoin::MyCoin(QString imgPath){
    QPixmap pix;
    bool ret = pix.load(imgPath);
    if(!ret){
        qDebug() << "图片加载失败" << imgPath;
        return;
    }

    this->setFixedSize(pix.width(),pix.height());
    this->setStyleSheet("QPushButton{border:0px;}");
    this->setIcon(pix);
    this->setIconSize(QSize(pix.width(),pix.height()));

    //创建两个定时器
    timer1 = new QTimer(this);
    timer2 = new QTimer(this);

    //监听两个定时器的timeout信号
    connect(timer1,&QTimer::timeout,this,[=](){
        QPixmap pix;
        QString str = QString(":res/Coin000%1").arg(this->min++);
        bool ret = pix.load(str);
        if(!ret){
            qDebug() << "图片加载失败:" << imgPath;
            return;
        }

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

        if(this->min > this->max){
            this->min = 1;//还原
            //动画的标志改为false；
            this->isAnimation = false;
            timer1->stop();
        }
    });

    //银币翻金币的定时器
    connect(timer2,&QTimer::timeout,this,[=](){
        QPixmap pix;
        QString str = QString(":res/Coin000%1").arg(this->max--);
        bool ret = pix.load(str);
        if(!ret){
            qDebug() << "图片加载失败:" << imgPath;
            return;
        }

        this->setFixedSize(pix.width(),pix.height());
        this->setStyleSheet("QPushButton{border:0px;}");
        this->setIcon(pix);
        this->setIconSize(QSize(pix.width(),pix.height()));

        if(this->max < this->min){
            this->max = 8;//还原
            //动画的标志改为false；
            this->isAnimation = false;
            timer2->stop();
        }
    });

}

//如果是金币  改成银币
void MyCoin::changeFlag(){
    if(this->flag){
        //启动金币翻银币的定时器
        timer1->start(30);

        //正在做动画
        this->isAnimation = true;
        this->flag = false;
    }else {
        timer2->start(30);
        //正在做动画
        this->isAnimation = true;
        this->flag = true;
    }
}

void MyCoin::mousePressEvent(QMouseEvent *e){
    if(this->isAnimation || this->isWin){
        return;
    }else{
        QPushButton::mousePressEvent(e);
    }
}

